package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;


   /*
     1、判断用户选择的体检日期，是否提供了体检服务。
     2、判断用户选择的体检日期，是否预约已满。
     3、判断用户是否是注册会员：
        不是：将其注册为会员
        是：判断用户提交的订单是否重复。（会员id,提交日期，体检套餐id判断）
     4、下单体验预约的订单
	 5、更新已预约人数
	*/
    @Override
    public Order add(Map map) throws Exception {

        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);

        //获取手机号码
        String telephone = (String) map.get("telephone");

        //判断用户预约的日期是否提供了预约服务
        OrderSetting orderSetting = orderSettingMapper.findByOrderDate(date);
        if (orderSetting==null){
            //没有提供预约服务，抛出异常提示用户
            throw new RuntimeException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断预约日期是否预约已满
        if (orderSetting.getReservations()>=orderSetting.getNumber()){
            //如果预约已满，抛出异常提示用户
            throw new RuntimeException(MessageConstant.ORDER_FULL);
        }

        //判断用户是否是注册会员
        Member member = memberMapper.findByTelephone(telephone);

        if (member==null){
            //如果不是会员，注册为会员
            member = new Member();
            member.setName((String)map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String)map.get("idCard"));
            member.setSex((String)map.get("sex"));
            member.setRegTime(new Date());
            //执行注册
            memberMapper.add(member);

        }else{
            //如果是会员，检查是否重复提交预约
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(date);
            order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));

            //查询是否重复提交预约
            Order queryOrder = orderMapper.findByCondition(order);
            //判断是否重复
            if (queryOrder!=null){
                //抛出异常，提示用户不要重复预约
                throw new RuntimeException(MessageConstant.HAS_ORDERED);
            }

        }

        Order order = new Order();
        order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
        order.setOrderDate(date);
        order.setMemberId(member.getId());
        order.setOrderType((String)map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);

        //执行新增
        orderMapper.add(order);

        //执行已预约数量的更新
        orderSettingMapper.updateReservationsByOrderDate(date);

        return order;
    }

    /**
     * 通过订单id查询订单信息
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) throws Exception {

        Map map = orderMapper.findById(id);
        Date orderDate = (Date) map.get("orderDate");
        map.put("orderDate",DateUtils.parseDate2String(orderDate));

        return map;
    }
}
